home *** CD-ROM | disk | FTP | other *** search
/ L' Effet Pommier 2 / L'Effet Pommier - Volume 02.iso / Echecs / GNU Chess 3.0 / Gnu Chess Source / file.c < prev    next >
Text File  |  1992-03-10  |  8KB  |  317 lines

  1. /*
  2.   Mac interface for GNU Chess
  3.  
  4.   Revision: 10 Feb 1991
  5.  
  6.   Copyright (C) 1986, 1987, 1988 Free Software Foundation, Inc.
  7.   Copyright (c) 1991  Airy ANDRE
  8.  
  9.     expanded game save, list, and restore features
  10.     optional auto-updating of positional information
  11.  
  12.   This file is part of CHESS.
  13.  
  14.   CHESS is distributed in the hope that it will be useful,
  15.   but WITHOUT ANY WARRANTY.  No author or distributor
  16.   accepts responsibility to anyone for the consequences of using it
  17.   or for whether it serves any particular purpose or works at all,
  18.   unless he says so in writing.  Refer to the CHESS General Public
  19.   License for full details.
  20.  
  21.   Everyone is granted permission to copy, modify and redistribute
  22.   CHESS, but only under the conditions described in the
  23.   CHESS General Public License.   A copy of this license is
  24.   supposed to have been given to you along with CHESS so you
  25.   can know your rights and responsibilities.  It should be in a
  26.   file named COPYING.  Among other things, the copyright notice
  27.   and this notice must be preserved on all copies.
  28.   */
  29.  
  30. #include <stdio.h>
  31. #include "math.h"
  32.  
  33. #include "DragMgr.h"
  34. #include "gnuchess.h"
  35. #include "macintf.h"
  36. #include "rsrc.h"
  37.  
  38. void
  39. GetOpenings ()
  40.  
  41. /*
  42.   Read in the Opening Book file and parse the algebraic notation for a
  43.   move into an unsigned integer format indicating the from and to
  44.   square. Create a linked list of opening lines of play, with
  45.   entry->next pointing to the next line and entry->move pointing to a
  46.   chunk of memory containing the moves. More Opening lines of up to 256
  47.   half moves may be added to gnuchess.book.
  48.   */
  49.  
  50. {
  51.   FILE *fd;
  52.   int c, i, j, side;
  53.   struct BookEntry *entry;
  54.   unsigned short mv, *mp, tmp[100];
  55.  
  56.   if (((fd = fopen (BOOK, "r")) != NULL) ||
  57.       ((fd = fopen ("gnuchess.book", "r")) != NULL))
  58.     {
  59.       Book = NULL;
  60.       i = 0;
  61.       side = white;
  62.       while ((c = parse (fd, &mv, side)) >= 0)
  63.     if (c == 1)
  64.       {
  65.         tmp[++i] = mv;
  66.         side = otherside[side];
  67.       }
  68.     else if (c == 0 && i > 0)
  69.       {
  70.         entry = (struct BookEntry *) malloc (sizeof (struct BookEntry));
  71.         mp = (unsigned short *) malloc ((i + 1) * sizeof (unsigned short));
  72.         if (entry!=NULL && mp!=NULL) {
  73.             entry->mv = mp;
  74.             entry->next = Book;
  75.             Book = entry;
  76.             for (j = 1; j <= i; j++)
  77.               *(mp++) = tmp[j];
  78.             *mp = 0;
  79.             i = 0;
  80.             side = white;
  81.         } else {
  82.             if (entry != NULL) free(entry);
  83.             if (mp != NULL) free(mp);
  84.         }
  85.       }
  86.       fclose (fd);
  87.     }
  88. }
  89.  
  90.  
  91. int
  92. parse (fd, mv, side)
  93.      FILE *fd;
  94.      short unsigned int *mv;
  95.      short int side;
  96. {
  97.   int c, i, r1, r2, c1, c2;
  98.   char s[100];
  99.   while ((c = getc (fd)) == ' ') ;
  100.   i = 0;
  101.   s[0] = c;
  102.   while (c != ' ' && c != '\n' && c != EOF)
  103.     s[++i] = c = getc (fd);
  104.   s[++i] = '\0';
  105.   if (c == EOF)
  106.     return (-1);
  107.   if (s[0] == '!' || s[0] == ';' || i < 3)
  108.     {
  109.       while (c != '\n' && c != EOF)
  110.     c = getc (fd);
  111.       return (0);
  112.     }
  113.   if (s[4] == 'o')
  114.     if (side == black)
  115.       *mv = 0x3C3A;
  116.     else
  117.       *mv = 0x0402;
  118.   else if (s[0] == 'o')
  119.     if (side == black)
  120.       *mv = 0x3C3E;
  121.     else
  122.       *mv = 0x0406;
  123.   else
  124.     {
  125.       c1 = s[0] - 'a';
  126.       r1 = s[1] - '1';
  127.       c2 = s[2] - 'a';
  128.       r2 = s[3] - '1';
  129.       *mv = (locn(r1, c1) << 8) + locn(r2, c2);
  130.     }
  131.   return (1);
  132. }
  133.  
  134. void
  135. GetGame ()
  136. {
  137.   int i;
  138.   long count;
  139.   SFTypeList list;
  140.   Point where;
  141.   SFReply reply;
  142.   int ref;
  143.   FileHeader header;
  144.   
  145.   where.h = 80; where.v = 90;
  146.   list[0] = 'GCsg';
  147.   SFGetFile(where, "\p", 0L, 1, list, 0L, &reply);
  148.   if (!reply.good) return;
  149.   
  150.   LDelRow(0,0,List);
  151.  
  152.   if ( FSOpen( reply.fName, reply.vRefNum, &ref ) == noErr) {
  153.         SetCursor(*ClockCursor);
  154.         count = sizeof(header);
  155.         FSRead( ref, &count, &header);
  156.         if (header.version == CUR_VERSION && header.signature == SIGNATURE) {
  157.             count = sizeof(drawn);
  158.             FSRead( ref, &count, &drawn);
  159.             count = sizeof(mate);
  160.             FSRead( ref, &count, &mate);
  161.             count = sizeof(towho);
  162.             FSRead( ref, &count, &towho);
  163.             count = sizeof(computer);
  164.             FSRead( ref, &count, &computer);
  165.             count = sizeof(opponent);
  166.             FSRead( ref, &count, &opponent);
  167.             count = sizeof(castld);
  168.             FSRead( ref, &count, castld);
  169.             count = sizeof(TCflag);
  170.             FSRead( ref, &count, TCflag);
  171.             count = sizeof(OperatorTime);
  172.             FSRead( ref, &count, OperatorTime);
  173.             count = sizeof(TimeControl);
  174.             FSRead( ref, &count, &TimeControl);
  175.             count = sizeof(GameCnt);
  176.             FSRead( ref, &count, &GameCnt);
  177.             count = sizeof(color);
  178.             FSRead( ref, &count, color);
  179.             count = sizeof(board);
  180.             FSRead( ref, &count, board);
  181.             count = sizeof(Mvboard);
  182.             FSRead( ref, &count, Mvboard);
  183.             count = 500 * sizeof(struct GameRec);
  184.             FSRead( ref, &count, GameList);
  185.             FSClose( ref );
  186.             InitCursor();
  187.         } else {
  188.             FSClose( ref );
  189.             InitCursor();
  190.             return;
  191.         }
  192.   }
  193.   
  194.   preview = 0;
  195.   InitializeStats ();
  196.   Sdepth = 0;
  197.   for (i=0; i<64; i++) {
  198.       saveColor[i] = color[i];
  199.       saveBoard[i] = board[i];
  200.   }
  201.   for (i=0; i<=GameCnt; i++) {
  202.       algbr(GameList[i].gmove >> 8,GameList[i].gmove & 0xFF,GameList[i].flags);
  203.       AddMove(i, mvstr[(GameList[i].flags & cstlmask) != 0]);
  204.   }
  205.   UpdateDisplay (0, 0, 1, 0, 1, color, board);
  206. }
  207.  
  208. void SaveGame()
  209. {
  210.     Point where;
  211.     SFReply reply;
  212.     long count;
  213.     int outRef;
  214.     FileHeader header;
  215.     Str255 fileName;
  216.     
  217.     where.h = 100; where.v = 50;
  218.     
  219.     header.version = CUR_VERSION;
  220.     header.signature = SIGNATURE;
  221.     GetWTitle(WindBoard, fileName);
  222.     SFPutFile(where, "", fileName, 0L, &reply);
  223.     if (!reply.good) return;
  224.     FSDelete(reply.fName, reply.vRefNum);
  225.     Create( reply.fName, reply.vRefNum, 'GCHS', 'GCsg' );
  226.     if ( FSOpen( reply.fName, reply.vRefNum, &outRef ) == noErr) {
  227.         SetCursor(*ClockCursor);
  228.         count = sizeof(header);
  229.         FSWrite( outRef, &count, &header);
  230.         count = sizeof(drawn);
  231.         FSWrite( outRef, &count, &drawn);
  232.         count = sizeof(mate);
  233.         FSWrite( outRef, &count, &mate);
  234.         count = sizeof(towho);
  235.         FSWrite( outRef, &count, &towho);
  236.         count = sizeof(computer);
  237.         FSWrite( outRef, &count, &computer);
  238.         count = sizeof(opponent);
  239.         FSWrite( outRef, &count, &opponent);
  240.         count = sizeof(castld);
  241.         FSWrite( outRef, &count, castld);
  242.         count = sizeof(TCflag);
  243.         FSWrite( outRef, &count, TCflag);
  244.         count = sizeof(OperatorTime);
  245.         FSWrite( outRef, &count, OperatorTime);
  246.         count = sizeof(TimeControl);
  247.         FSWrite( outRef, &count, &TimeControl);
  248.         count = sizeof(GameCnt);
  249.         FSWrite( outRef, &count, &GameCnt);
  250.         count = sizeof(color);
  251.         FSWrite( outRef, &count, color);
  252.         count = sizeof(board);
  253.         FSWrite( outRef, &count, board);
  254.         count = sizeof(Mvboard);
  255.         FSWrite( outRef, &count, Mvboard);
  256.         count = 500 * sizeof(struct GameRec);
  257.         FSWrite( outRef, &count, GameList);        
  258.         FSClose( outRef );
  259.         InitCursor();
  260.     }
  261. }
  262.  
  263. int FSWriteStr(int ref, char * str)
  264. {
  265.     long count;
  266.     
  267.     count = *str;
  268.     return FSWrite( ref, &count, str); 
  269. }
  270.  
  271. void
  272. ListGame ()
  273. {
  274.     Point where;
  275.     int i, outRef;
  276.     SFReply reply;
  277.     Str255 fileName;
  278.     char bufout[100];
  279.     
  280.      where.h = 100; where.v = 50;
  281.     
  282.     GetWTitle(WindList, fileName);
  283.     SFPutFile(where, "", fileName, 0L, &reply);
  284.     if (!reply.good) return;
  285.     FSDelete(reply.fName, reply.vRefNum);
  286.     Create( reply.fName, reply.vRefNum, 'GCHS', 'TEXT' );
  287.     if ( FSOpen( reply.fName, reply.vRefNum, &outRef ) == noErr) {
  288.         SetCursor(*ClockCursor);
  289.  
  290.         FSWriteStr( outRef, "\p\r");
  291.           FSWriteStr( outRef, *(char **)GetString(_LISTHEAD_STR));
  292.           FSWriteStr( outRef, "\p         ");
  293.           FSWriteStr( outRef, *(char **)GetString(_LISTHEAD_STR));
  294.         FSWriteStr( outRef, "\p\r");
  295.           for (i = 0; i <= GameCnt; i++)
  296.             {
  297.                 int f,t;
  298.                 
  299.               f = GameList[i].gmove >> 8;
  300.               t = (GameList[i].gmove & 0xFF);
  301.               algbr (f, t, GameList[i].flags);
  302.               if ((i % 2) == 0)
  303.                 FSWriteStr( outRef, "\p\r");
  304.               else
  305.                 FSWriteStr ( outRef, "\p         ");
  306.                   sprintf (bufout+1, "%5s  %5d     %2d %7ld %5d", mvstr[0],
  307.                    GameList[i].score, GameList[i].depth,
  308.                    GameList[i].nodes, GameList[i].time);
  309.              *bufout = strlen(bufout+1);
  310.              FSWriteStr( outRef, bufout);
  311.         }
  312.          FSWriteStr ( outRef, "\p\r\r");
  313.         FSClose( outRef );
  314.         InitCursor();
  315.     }
  316. }
  317.